package me.zixing.memorandum.ui.component;

import me.zixing.memorandum.domain.DayTask;
import me.zixing.memorandum.utils.AppConstant;
import me.zixing.memorandum.utils.JdbcUtils;
import me.zixing.memorandum.utils.TaskPanelUtils;
import me.zixing.memorandum.utils.TimingUtils;
import org.jb2011.lnf.beautyeye.ch3_button.BEButtonUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.text.SimpleDateFormat;

public class FormPanel extends JFrame {
    private TheDayPanel theDayPanel;
    private JTextField titleFiled;
    private JButton OKButton;
    private JComboBox<String> startHourComboBox;
    private JComboBox<String> startMinComboBox;
    private JComboBox<String> endHourComboBox;
    private JComboBox<String> endMinComboBox;
    private int limit = 15;
    private int hourCount = 24;
    private int minCount = 60;
    private DayTask dayTask;
    private JViewport jViewport;
    private SimpleDateFormat YMDFormat = new SimpleDateFormat("yyyy-MM-dd ");

    public FormPanel(TheDayPanel dayPanel){
        if(dayPanel != null){
            this.theDayPanel = dayPanel;
        }
        initUI();
        initAction();
        this.setSize(310,440);
        this.setLayout(null);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    public FormPanel(DayTask dayTask,JViewport jViewport){
        this.dayTask = dayTask;
        this.jViewport = jViewport;
        initUI();
        initAction();
        this.setSize(310,440);
        this.setLayout(null);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    private void initUI(){
        JPanel mainPanel = new JPanel();
        mainPanel.setBounds(0,0,300,410);
        mainPanel.setLayout(null);
        //备忘内容
        /*备忘开始*/
        JPanel contentPanel = new JPanel();
        contentPanel.setBounds(0,10,300,80);
        contentPanel.setLayout(null);

        JLabel titleLable = new JLabel("备忘");
        titleLable.setFont(AppConstant.SOFT_BOLD_15);
        titleLable.setBounds(22,0,50,80);

        titleFiled = new JTextField();
        titleFiled.setBounds(62,20,200,40);
        titleFiled.setFont(AppConstant.SOFT_BOLD_15);

        if(dayTask!=null){
            titleFiled.setText(dayTask.getTitle());
        }

        contentPanel.add(titleFiled);
        contentPanel.add(titleLable);

        /*备忘结束*/

        //开始时间选择
        /*开始选择 开始*/
        JPanel startPanel = new JPanel();
        startPanel.setBounds(0,100,300,95);
        startPanel.setLayout(null);

        JLabel label = new JLabel("开始时间");
        label.setBounds(22,0,100,30);
        label.setFont(AppConstant.SOFT_BOLD_15);
        startPanel.add(label);

        startHourComboBox = new JComboBox<>();
        startHourComboBox.setSize(80,20);
        startHourComboBox.setLocation(20,40);
        for(int i=0;i<hourCount;i++){
            if(String.valueOf(i).length() == 1){
                startHourComboBox.addItem("0"+String.valueOf(i));
            }else{
                startHourComboBox.addItem(String.valueOf(i));
            }
        }

        JLabel startHourLabel = new JLabel("时");
        startHourLabel.setBounds(118,40,20,20);
        startPanel.add(startHourLabel);

        startMinComboBox = new JComboBox<>();
        startMinComboBox.setSize(80,20);
        startMinComboBox.setLocation(150,40);
        for(int i=0;i<minCount;i++){
            if(String.valueOf(i).length() == 1){
                startMinComboBox.addItem("0"+String.valueOf(i));
            }else{
                startMinComboBox.addItem(String.valueOf(i));
            }
        }

        JLabel startMinLabel = new JLabel("分");
        startMinLabel.setBounds(250,40,20,20);
        startPanel.add(startMinLabel);
        startPanel.add(startHourComboBox);
        startPanel.add(startMinComboBox);
        /*开始选择 结束*/

        //结束时间选择
        JPanel endPanel = new JPanel();
        endPanel.setBounds(0,205,300,95);
        endPanel.setLayout(null);

        JLabel endLabel = new JLabel("结束时间");
        endLabel.setBounds(22,0,100,30);
        endLabel.setFont(AppConstant.SOFT_BOLD_15);
        endPanel.add(endLabel);

        endHourComboBox = new JComboBox<>();
        endHourComboBox.setSize(80,20);
        endHourComboBox.setLocation(20,40);
        for(int i=0;i<hourCount;i++){
            if(String.valueOf(i).length() == 1){
                endHourComboBox.addItem("0"+String.valueOf(i));
            }else{
                endHourComboBox.addItem(String.valueOf(i));
            }
        }

        JLabel endHourLabel = new JLabel("时");
        endHourLabel.setBounds(118,40,20,20);
        endPanel.add(endHourLabel);

        endMinComboBox = new JComboBox<>();
        endMinComboBox.setSize(80,20);
        endMinComboBox.setLocation(150,40);
        for(int i=0;i<minCount;i++){
            if(String.valueOf(i).length() == 1){
                endMinComboBox.addItem("0"+String.valueOf(i));
            }else{
                endMinComboBox.addItem(String.valueOf(i));
            }
        }

        JLabel endMinLabel = new JLabel("分");
        endMinLabel.setBounds(250,40,20,20);
        endPanel.add(endMinLabel);
        endPanel.add(endHourComboBox);
        endPanel.add(endMinComboBox);
        /*开始选择 结束*/


        if(dayTask != null){
            String[] startTimeSplit = dayTask.getStartDateTime().split(":");
            startHourComboBox.setSelectedItem(startTimeSplit[0]);
            startMinComboBox.setSelectedItem(startTimeSplit[1]);
            String[] endTimeSplit = dayTask.getEndDateTime().split(":");
            endHourComboBox.setSelectedItem(endTimeSplit[0]);
            endMinComboBox.setSelectedItem(endTimeSplit[1]);
        }

        //按钮panel
        JPanel buttonPanel = new JPanel();
        buttonPanel.setBounds(0,310,300,80);
        buttonPanel.setLayout(null);

        OKButton = new JButton("确定");
        OKButton.setBounds(120,25,60,30);
        OKButton.setForeground(Color.white);
        OKButton.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.blue));

        buttonPanel.add(OKButton);

        mainPanel.add(contentPanel);
        mainPanel.add(startPanel);
        mainPanel.add(endPanel);
        mainPanel.add(buttonPanel);
        this.add(mainPanel);
    }

    private void initAction(){
        titleFiled.addKeyListener(new KeyAdapter() {

            @Override
            public void keyTyped(KeyEvent e) {
                if(titleFiled.getText().length() >= FormPanel.this.limit){
                    e.consume();
                }
            }
        });
        if(theDayPanel != null){
            OKButton.addActionListener(new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd ");
                    DayTask dayTask = new DayTask();
                    dayTask.setTitle(FormPanel.this.titleFiled.getText().trim());
                    String startDateTime = format.format(theDayPanel.getDayDO().getDate())
                            +startHourComboBox.getSelectedItem().toString() + ":"
                            +startMinComboBox.getSelectedItem().toString() + ":"
                            +"00";
                    String endDateTime = format.format(theDayPanel.getDayDO().getDate())
                            +endHourComboBox.getSelectedItem().toString() + ":"
                            +endMinComboBox.getSelectedItem().toString() + ":"
                            +"00";
                    dayTask.setStartDateTime(startDateTime);
                    dayTask.setEndDateTime(endDateTime);
                    dayTask.setPriority(1);
                    dayTask.setDayDO(FormPanel.this.theDayPanel.getDayDO());

                    JdbcUtils.addDayTask(dayTask);
                    TaskPanelUtils.refreshTasksPanel(theDayPanel.getjViewport(),FormPanel.this.theDayPanel.getDayDO());
                    theDayPanel.repaint();
                    FormPanel.this.dispose();

                }
            });

        }else if(dayTask !=null){
            OKButton.addActionListener(new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    dayTask.setTitle(FormPanel.this.titleFiled.getText().trim());
                    dayTask.setPriority(1);
                    String startTime = startHourComboBox.getSelectedItem().toString() + ":"
                            +startMinComboBox.getSelectedItem().toString() ;
                    String endTime = endHourComboBox.getSelectedItem().toString() + ":"
                            +endMinComboBox.getSelectedItem().toString() ;

                    dayTask.setStartDateTime(startTime);
                    dayTask.setEndDateTime(endTime);

                    JdbcUtils.updateDayTask(dayTask);
                    for (DayTask setDayTask: TimingUtils.dayTasks) {
                        if(setDayTask.getId() == dayTask.getId()){
                            setDayTask = dayTask;
                        }
                    }
                    TaskPanelUtils.refreshTasksPanel(jViewport,dayTask.getDayDO());
                    FormPanel.this.dispose();
                }
            });
        }


    }

}
